package bTree.LeetCode;

public class 删除排序链表中的重复元素2 {
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null) {
            return head;
        }
        ListNode dummy = new ListNode(101, null);
        ListNode leftNode = dummy, rightNode = head, preNode = dummy;
        while (rightNode.next != null) {
            if (rightNode.val == preNode.val || rightNode.val == rightNode.next.val) {
                preNode = rightNode;
                rightNode = rightNode.next;
            } else {
                leftNode.next = rightNode;
                leftNode = rightNode;
                preNode = rightNode;
                rightNode = rightNode.next;
                leftNode.next = null;
            }
        }
        if (rightNode.val != preNode.val) {
            leftNode.next = rightNode;
        }
        return dummy.next;
    }
}